package com.test.google.app.service.impl;

import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;

import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.test.google.app.domain.File;
import com.test.google.app.service.FileService;
import com.test.google.app.util.GaeMemcacheUtil;
import com.test.google.app.util.JPAMng;

public class FileServiceImpl implements FileService {
	
	private DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();

	@Override
	public Iterator<Entity> list(String fileName) {
		Query q = new Query(BlobInfoFactory.KIND);
		q.setFilter(new FilterPredicate(
				BlobInfoFactory.FILENAME, FilterOperator.EQUAL, fileName));
		return datastoreService.prepare(q).asIterable().iterator();
	}

	@Override
	public void delete(Key key) {
		EntityManager em = JPAMng.getEMF().createEntityManager();
//		em.remove(new File(key));
		em.createQuery("delete from File t where t.key=?1").setParameter(1, key).executeUpdate();
		em.close();
	}

	@Override
	public void deleteAll() {
		EntityManager em = JPAMng.getEMF().createEntityManager();
		em.createQuery("delete from File t").executeUpdate();
		em.close();
		/*DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
		Iterable<Entity> files = datastoreService.prepare(new Query(BlobInfoFactory.KIND)).asIterable();
		List<BlobKey> keys = new ArrayList<BlobKey>();
		for (Entity e : files) {
			String fileName = (String) e.getProperty(BlobInfoFactory.FILENAME);
			MemcacheServiceFactory.getMemcacheService().delete("blobKey_" + fileName);
			keys.add(new BlobKey(e.getKey().getName()));
		}
		BlobstoreServiceFactory.getBlobstoreService().delete(keys.toArray(new BlobKey[keys.size()]));*/
	}

	@SuppressWarnings("unchecked")
	@Override
	public void saveOrUpdateByName(File file) {
		EntityManager em = JPAMng.getEMF().createEntityManager();
		List<File> list = em.createQuery("select t from File t where t.path=?1").
				setParameter(0, file.getPath()).getResultList();  
		if (list.isEmpty()) {
			em.persist(file);
		} else {
			file.setKey(list.get(0).getKey());
			em.merge(file);
			
			GaeMemcacheUtil.del(file.getPath());
		}
		em.close();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<File> getAll() {
		EntityManager em = JPAMng.getEMF().createEntityManager();
		List<File> list = em.createQuery("select t from File t order by t.path").getResultList();
		em.close();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<File> getByPath(String path) {
		EntityManager em = JPAMng.getEMF().createEntityManager();
		List<File> list = em.createQuery("select t from File t where t.path=?1")
				.setParameter(1, path).getResultList();
		em.close();
		return list;
	}
	
	@Override
	public byte[] getContentByKey(Key key) {
		EntityManager em = JPAMng.getEMF().createEntityManager();
		File file = (File) em.createQuery("select t from File t where t.key=?1")
				.setParameter(1, key).getSingleResult();
		byte[] bb = file.getContent();
		em.close();
		return bb;
	}

}
